//sm2国密加密
export function doEncrypt(msgString) {
    let msg = msgString;
    if (typeof (msgString) !== 'string') {
      msg = JSON.stringify(msgString);
    }
    let sm2 = require('sm-crypto').sm2;
    console.log(sm2,"sm2---------------------")
    // 1 - C1C3C2；	0 - C1C2C3；	默认为1
    let cipherMode = 1
    //let publicKey2 = 'MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEKlNIQ4FsiGYR9MFQBz3uBo1DzkzyI5P6z0hGkQN+IN1fFBYMMnzVqV1LYnUjc7DouIqWEML+QLfBbVXWNjr60A==' //公2
    let a="0419d40bb82dca774fc0e54a06998977b26a7d7443899932cfc24d1452a6cf3200588a5fee1a6c8d23ceddcb593d89a3aecc521225738301b1b4d54dac0f56aa14"
    // 加密结果
    let encryptData = sm2.doEncrypt(msg, a, cipherMode); 
    // 加密后的密文前需要添加04，后端才能正常解密
    let encrypt = 'WeidaEncrypt:04' + encryptData;
    return encrypt;
}

// sm2国密解密
export function doDecryptStr(enStr){
    let msg = enStr;
    if (typeof (enStr) !== 'string') {
      msg = JSON.stringify(enStr);
    }
  
    let sm2 = require('sm-crypto').sm2;
    // 1 - C1C3C2；	0 - C1C2C3；	默认为1
    let cipherMode = 1
    //privateKey1
    let a = '5654ba2b4b9f41c0a87d4039782d8ae19248ba989cf18278bc914ae3402fcb43'//私1
    // 加密后的密文，需要前去掉04。因为doDecrypt中自行添加了04，后端加密代码也自行添加了04

   
    let en = enStr.substring(2)
    // 解密结果
    let doDecrypt = sm2.doDecrypt(en, a, cipherMode); 
    // 解密后类型转换
    let objData = JSON.parse(doDecrypt)
    return objData;
  }
  